package cn.iocoder.an.module.business.controller.admin.equipmenttype;

import cn.iocoder.an.module.business.controller.admin.equipmentkeeptype.vo.EquipmentKeepTypeImportExcelVO;
import cn.iocoder.an.module.business.controller.admin.equipmentkeeptype.vo.EquipmentKeepTypeRespVO;
import cn.iocoder.an.module.business.controller.admin.equipmentmanage.vo.ImportRespVO;
import cn.iocoder.an.module.business.enums.MaintenanceStatusEnum;
import cn.iocoder.an.module.business.service.equipmentkeepconfig.EquipmentKeepConfigService;
import cn.iocoder.an.module.business.service.equipmentkeeptype.EquipmentKeepTypeService;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.equipmenttype.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.equipmenttype.EquipmentTypeDO;
import cn.iocoder.an.module.business.service.equipmenttype.EquipmentTypeService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 设备分类")
@RestController
@RequestMapping("/business/equipment-type")
@Validated
public class EquipmentTypeController {

    @Resource
    private EquipmentTypeService equipmentTypeService;

    @Resource
    private EquipmentKeepConfigService equipmentKeepConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建设备分类")
    @PreAuthorize("@ss.hasPermission('business:equipment-type:create')")
    public CommonResult<Long> createEquipmentType(@Valid @RequestBody EquipmentTypeSaveReqVO createReqVO) {
        return success(equipmentTypeService.createEquipmentType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备分类")
    @PreAuthorize("@ss.hasPermission('business:equipment-type:update')")
    public CommonResult<Boolean> updateEquipmentType(@Valid @RequestBody EquipmentTypeSaveReqVO updateReqVO) {
        equipmentTypeService.updateEquipmentType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:equipment-type:delete')")
    public CommonResult<Boolean> deleteEquipmentType(@RequestParam("id") Long id) {
        equipmentTypeService.deleteEquipmentType(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除设备分类")
                @PreAuthorize("@ss.hasPermission('business:equipment-type:delete')")
    public CommonResult<Boolean> deleteEquipmentTypeList(@RequestParam("ids") List<Long> ids) {
        equipmentTypeService.deleteEquipmentTypeListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:equipment-type:query')")
    public CommonResult<EquipmentTypeRespVO> getEquipmentType(@RequestParam("id") Long id) {
        EquipmentTypeDO equipmentType = equipmentTypeService.getEquipmentType(id);
        return success(BeanUtils.toBean(equipmentType, EquipmentTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备分类分页")
    @PreAuthorize("@ss.hasPermission('business:equipment-type:query')")
    public CommonResult<PageResult<EquipmentTypeRespVO>> getEquipmentTypePage(@Valid EquipmentTypePageReqVO pageReqVO) {
        PageResult<EquipmentTypeDO> pageResult = equipmentTypeService.getEquipmentTypePage(pageReqVO);
        PageResult<EquipmentTypeRespVO> bean = BeanUtils.toBean(pageResult, EquipmentTypeRespVO.class);
        bean.getList().forEach(item -> {
            String allName = equipmentKeepConfigService.getAllName(1, item.getId());
            item.setKeepConfigName(allName);
        });
        return success(bean);
    }


    @GetMapping("/getAllList")
    @Operation(summary = "获得所有设备分类")
    public CommonResult<List<EquipmentTypeDO>> getEquipmentTypeList() {
        return success(equipmentTypeService.getEquipmentTypeList());
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备分类 Excel")
    @PreAuthorize("@ss.hasPermission('business:equipment-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEquipmentTypeExcel(@Valid EquipmentTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EquipmentTypeDO> list = equipmentTypeService.getEquipmentTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备分类.xls", "数据", EquipmentTypeRespVO.class,
                        BeanUtils.toBean(list, EquipmentTypeRespVO.class));
    }


    @GetMapping("/get-import-template")
    @Operation(summary = "获得保养设置导入模板导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<EquipmentTypeImportExcelVO> list = Arrays.asList(
                EquipmentTypeImportExcelVO.builder().name("打孔设备").description("xxxxx描述").build()
        );
        // 输出
        ExcelUtils.write(response, "设备分类导入模板.xlsx", "列表", EquipmentTypeImportExcelVO.class, list);
    }


    @PostMapping("/import")
    @Operation(summary = "导入")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    public CommonResult<ImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                  @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<EquipmentTypeImportExcelVO> list = ExcelUtils.read(file, EquipmentTypeImportExcelVO.class);
        return success(equipmentTypeService.importList(list, updateSupport));
    }

}